package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.cfca.CfcaConfiguration;
import com.shell.bee.cfca.paperless.request.CfcaBatchSignRequest;
import com.shell.bee.cfca.paperless.request.CfcaBatchSignRequest.BatchSignParam;
import com.shell.bee.cfca.paperless.response.BaseResponse;
import com.shell.bee.cfca.paperless.service.CfcaSignTxService;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.bee.report.utils.FilePathHelper;
import com.shell.feign.PubUserFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.user.po.PubUser;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.esign.service.BatchSignService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSerFeeZYBatchSignSubscriber extends OnErrorNotImplementedSubscriber<BatchBillEvent<String, CbDct>> {
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private BatchSignService batchSignService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private ContService contService;
	@Autowired
	private PubUserFeign pubUserFeign;
	@Autowired
	private CfcaSignTxService cfcaSignTxService;
	private static final String REASON = "����������������";

	public void onApplicationEvent(BatchBillEvent<String, CbDct> t) {
		List<String> signBillIds = new ArrayList();
		String signBillId = null;
		List<String> records = t.getRecs();
		for (String record : records) {
			signBillId = record;
			BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(record, false);

			List<Bill<CbDct>> children = (List<Bill<CbDct>>) event.getChildren();
			if ((children != null) && (children.size() > 0)) {
				List<Bill<CbDct>> bills = children;
				for (Iterator localIterator2 = bills.iterator(); localIterator2.hasNext();) {
					Bill<CbDct> dctBill = (Bill) localIterator2.next();
					CbDct dct = (CbDct) dctBill.getSource();
					if ((dct != null) && (dct.getDctId() != null)) {
						if (!dct.getBuyerId().equals(dct.getSellerId())) {
							break;
						}
						signBillId = dct.getBillId();
					}
				}
			}
			signBillIds.add(signBillId);
		}
		CusRec artisan = this.cusRecService.getArtisan(null);
		String cusId = artisan.getCusId();
		PubUser user = this.pubUserFeign.selectByKey(CommonDataUtil.getUserId());
		List<ContBas> conts = new ArrayList();
		CfcaBatchSignRequest request = new CfcaBatchSignRequest();
		CfcaBatchSignRequest.BatchSignParam batchSignParam = null;
		for (Iterator<String> dctBill = signBillIds.iterator(); dctBill.hasNext();) {
			String billId = dctBill.next();
			ContBas cont = this.contService.getLatestCont(cusId, billId, "51", null);
			if (cont == null) {
				throw new UnCheckMsgException("����������");
			}
			conts.add(cont);
			List<ContProt> prots = null;
			prots = this.contService.selectProts(cont.getContId());
			if ((prots != null) && (prots.size() > 0)) {
				ContProt contProt = (ContProt) prots.get(0);
				ContTmpl contTmpl = this.contService.selectContTmplByKey(contProt.getTmplId());
				batchSignParam = new CfcaBatchSignRequest.BatchSignParam();
				batchSignParam.setSignPage(Integer.parseInt(contTmpl.getAcceptorPage()));
				batchSignParam.setKeyIndex(contTmpl.getAcceptorKeyIndex());
				batchSignParam.setKey(artisan.getCusName());
				batchSignParam.setContId(cont.getContId());
				batchSignParam.setSrcPdfPath(contProt.getProtLoc());
				batchSignParam.setDestPdfPath(
						FilePathHelper.replaceFileName(contProt.getProtLoc(), SystemUtil.getUUID(), "pdf"));
				batchSignParam.setProtId(contProt.getProtId());
				request.addBatchSignParam(batchSignParam);
			}
		}
		String billId;
		request.setUserId(user.getUserId());
		request.setUserName(user.getUserName());
		request.setIdNo(user.getCertNo());
		request.setCusId(cusId);
		request.setCusName(artisan.getCusName());
		request.setReason("����������������");
		request.setSealCode(CfcaConfiguration.getCfcaZySealCode());

		BaseResponse response = this.cfcaSignTxService.batchSign(request);
		if (!response.isSuccess()) {
			throw new UnCheckMsgException("������������������cfca��" + response.getMessage());
		}
		this.batchSignService.updateConts(conts, cusId);
		for (CfcaBatchSignRequest.BatchSignParam signParam : request.getBatchSignParams()) {
			this.batchSignService.updateProtLoc(signParam.getProtId(), signParam.getSrcPdfPath(),
					signParam.getDestPdfPath());
		}
	}
}
